#pragma once

#include <vector>

#include "ShmService.hpp"
#include "Common.hpp"

class ShmServiceNode
{
public:
    ShmService::ShmServicePOSIX* _shmSvr;
    int32_t _shmFileId;
    std::vector<Span*> _pageMap;

    ShmServiceNode(uint32_t size, const std::string& shmFileName, int32_t count, bool isCreate)
        : _shmFileId(count) {
        if (_shmFileId < 0) {
            LOG_CYC(ERROR, "文件计数错误，count：%d", _shmFileId);
            exit(1);
        }

        if (isCreate) {
            _shmSvr = ShmService::ShmServicePOSIX::GetInstance(size, ShmService::SHM_CREATE, shmFileName.c_str());
            if (size > MAX_BYTES * (NPAGES - 1))
                _pageMap.resize(1, 0);
            else
                _pageMap.resize((MAX_BYTES * (NPAGES - 1)) >> PAGE_SHIFT, 0);
        }
        else {
            _shmSvr = ShmService::ShmServicePOSIX::GetInstance(size, ShmService::SHM_CONNECT, shmFileName.c_str());
        }

        if (!_shmSvr) {
            LOG_CYC(ERROR, "共享内存操作失败，文件名: %s", shmFileName.c_str());
            exit(2);
        }
    }

    ~ShmServiceNode() {
        if (_shmSvr) {
            delete _shmSvr;
        }
    }
};